<?


class CTRLtext extends CONTROL {

#	In parent class:
#	function __construct()
#	function ajax()

	
#	Update.
	function filter_update() {
		global $Engine;

		$key = ( $this->vars->html )? "long": "short";
		$lang = $this->vars->langfocus;
			
		$textid = $Engine->text->set( $this->vars->values->{ $lang }, (int) $this->vars->textid, $key, $lang );


		return ( $this->vars->textid )?
			$this->comment( "Updated $lang text" ) : $this->filter_new( $textid );
			
	}
	
#	New text.
	function filter_new( $textid ) {
		switch( $this->vars->type ){
		case "page" :
			$Object = new PAGE( $this->vars->parentid );
			break;
					
		case "module" :
			$Modcon = new MOD_CONSTRUCT();
			$Object = $Modcon->request_object( $this->vars->parentid );
			break;

		default: 
			$Object = new OBJECT( $this->vars->type, (int) $this->vars->parentid );
		}		
		
		if( $this->vars->field ) {

				$Object->values [ 'meta' ]->{ $this->vars->field } = $textid;
				$Object->set( "meta", $Object->values [ 'meta' ], true );
		
		} else {

			if(  $this->vars->html )
				 $setter = ($this->vars->type == 'news')? "contid": "textid";
			else $setter = 'nameid';

			$Object->set( $setter, (string) $textid);
		}
		
		return $textid;

	}

#	Dictionary request.
	function filter_dictionary() {
		
		unset( $_SESSION['init']['dictionary']->{'id'} );
		return json_encode( $_SESSION['init']['dictionary'] );
	}
	
#	Dictionary update.
	function filter_dictionary_update() {
		global $DB;
		
		if( $this->vars->is_key ) {
			$_SESSION['init']['dictionary']->{ $this->vars->value } = $_SESSION['init']['dictionary']->{ $this->vars->key };
			unset( $_SESSION['init']['dictionary']->{ $this->vars->key } );
		
		} else if( $this->vars->value )
			$_SESSION['init']['dictionary']->{ $this->vars->key }->{ $this->vars->lang } = $this->vars->value;
		else
			unset( $_SESSION['init']['dictionary']->{ $this->vars->key }->{ $this->vars->lang } );
			
		$DB->query( "update moddump set json='".json_encode( $_SESSION['init']['dictionary'] )."' where ID=" . engine_get( "lang.dictionary" ), 0 );
		
		return "dashboard.comment( '" .( $this->vars->is_key? "changed keyname" : "updated {$this->vars->key} ({$this->vars->lang})" ). "' )";
	}
	
	
# 	Google auto translate() {
	function filter_auto_translate() {
		global $DB;		
		
		// gtranslate and update session
		$this->vars->value = strtolower( $this->gtranslate( 'nl', $this->lang, $this->key ));	
		$_SESSION['init']['dictionary']->{ $this->key }->{ $this->lang } = $this->vars->value;	

		// db update
		$DB->query( "update moddump set json='".json_encode( $_SESSION['init']['dictionary'] )."' where ID=" . engine_get( "lang.dictionary" ), 0 );
		return $this->vars->value ;
	}

	
	/**
	 *	translator
	 *	the one and only function inside this class
	 *
	 *	@param		string - language in
	 *	@param		string - language out
	 *	@param		string - text string
	 *	@return		string
	 */
	function gtranslate( $in = 'fr', $out = 'en', $string ) {
	
		$url		= 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=';
	
		// define url and try to get content
		$url		= $url . rawurlencode( $string ) . '&langpair=' . rawurlencode( $in . '|' . $out );
		$content	= @file_get_contents( $url ) or die( "false" );
		
		// json decode and get string
		$json	= json_decode( $content );
		return $json->responseData->translatedText;
	
	}




}

?>